package com.tq110.web;


import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.databind.util.JSONPObject;
import com.mysql.jdbc.Driver;
import com.tq110.pojo.Music;
import com.tq110.pojo.PageBean;
import com.tq110.pojo.Student;
import com.tq110.pojo.TempBean;
import com.tq110.service.MusicService;
import com.tq110.util.Tqutil;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.aop.AopAutoConfiguration;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.boot.autoconfigure.web.servlet.WebMvcAutoConfiguration;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.sql.DataSource;
import java.io.File;
import java.sql.SQLException;
import java.util.*;

@Controller
@RequestMapping("/music")
//@Scope("prototype")
public class TestController  implements ApplicationContextAware {

    @Qualifier("musicService")
    @Autowired()
    private MusicService musicService;

    private ApplicationContext axtt;
    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.axtt=applicationContext;
    }

    private PageBean pageBean;

    public TestController() {
        System.out.println("======TestController实例化============");
    }
     //    192.168.0.105:8080/music/json

    @RequestMapping(value = "/json",produces = "application/json;charset=UTF-8")
    @ResponseBody
    public JSONObject  addMusic(String name, String age) throws SQLException {
        JSONObject  json=new JSONObject();
        JSONArray  arr01=new JSONArray();
        JSONObject  json2=new JSONObject();
        json2.put("game","和平精英");
        json2.put("education","全日制大学本科");
        json2.put("args",name+"==="+age);
        arr01.add(json2);
        json2=null;
        json2=new JSONObject();
        json2.put("girl","刘兰燕,薛淼佳,顾东东,污小顾");
        arr01.add(json2);
        json.put("id",UUID.randomUUID().toString());
        json.put("name","tq110");
        json.put("age","26");
        json.put("idcard","42092219********31");
        json.put("features",arr01);
       DataSource  dataSource =axtt.getBean(DataSource.class);
       // DruidDataSource fdg=null;
        System.out.println(dataSource+"=========fgbfd============="+dataSource.getClass());
         //printExeEnvironment();
        AopAutoConfiguration sds=null;
        System.out.println("==============ActiveProfiles111=================");
        for (String kk:axtt.getEnvironment().getActiveProfiles()) {
           // System.out.println(kk);
        }
        System.out.println("==============ActiveProfiles222=================");


        DataSourceAutoConfiguration  sd=null;
        return  json;
    }

    private void printExeEnvironment() {
        System.out.println("====================springboot执行环境========================");

        Properties  pp=System.getProperties();
        for (Map.Entry  entry:pp.entrySet()){
            System.out.println(entry.getKey()+"==="+entry.getValue());
        }
    }

    @RequestMapping("/addMusic")
    //@ResponseBody
    public String  addMusic(Music mcc){
        WebMvcAutoConfiguration at001;

        System.out.println("TestController.addMusic============rsegvesrb");
       String  ctime =Tqutil.fromDateToStr_common(mcc.getCreateTime());
            musicService.addMusic(mcc);
        System.out.println("TestController.addMusic=======add ok===="+ctime);
        return "none";
    }
    @RequestMapping("/select55")
    @ResponseBody
    public String  listMusic(Music mcc, HttpServletRequest  request){
        System.out.println("xdfnrdrngft================="+mcc+"===");
        /*
        http://192.168.0.105:8080/music/select55?page=1&pageSize=10&author=薛
        String  page=request.getParameter("page");
        String  pageSize=request.getParameter("pageSize");
        String  as=mcc.getAuthor();*/
        Map<String,String>  map=new HashMap<>();
        PageBean  pageBean=new PageBean();
       /* pageBean.setCurPage(Integer.valueOf(page));
        pageBean.setPageSize(Integer.valueOf(pageSize));*/
        map.put("author", mcc.getAuthor());
        map.put("page", request.getParameter("page"));
        map.put("pageSize", request.getParameter("pageSize"));
        System.out.println("sdbesfndrmtk========"+map);
         musicService.listAll(pageBean,map);

        return  "ok618616";
    }
    @RequestMapping("/add56")
    @ResponseBody
    public String mfrebrhbhting(){

       Music mccc =musicService.findById(2);
        System.out.println("TestController.mfrebrhbhting"+mccc);
        System.out.println("-rehnnje-----------"+mccc.getMcType());
        return  "serfbserh";
    }
    @RequestMapping("/add55")
    public String mfrehbhting(){
        System.out.println("dhdnfsrrj====nrreserfk==========g");
        return "add";
    }
   /*
   http://127.0.0.1:8080/img/img01.jpg
     http://127.0.0.1:8080/music/add55
     http://127.0.0.1:8080/music/demo22
   */
     @RequestMapping("/demo22")
    public ModelAndView hellodvdfv(ModelAndView  mv, HttpServletRequest request){
        List<Student> list=new ArrayList<>();
        list.add(new Student(23,"jack","17"));
        list.add(new Student(54,"马化腾","48"));
        list.add(new Student(346,"吴清函","16"));
        Map<String,Student> map22 =new HashMap<>();
        map22.put("uu1",new Student(23,"jack","17"));
        map22.put("u22",new Student(346,"吴清函","16"));
        map22.put("u3236",new Student(54,"马化腾","48"));
        Map<String,Object> map =new HashMap<>();
        map.put("msg","今天天气不错");
        map.put("msg2","");
        map.put("sex","男");
        map.put("game",new Random().nextInt(6));
        map.put("date",new Date());

         map.put("thkey5","你个Rng_tq");
         map.put("k33","lol-box");
         map.put("sb3","<h2>牛逼哄哄啊43364</h2>");
         map.put("users", Arrays.asList("zs","ls","ww","hl"));

        mv.addObject("data",map);
        mv.addObject("students",list);
        mv.addObject("mapDom",map22);


        mv.setViewName("demoThymeleaf");

        request.setAttribute("key1","v1");
        request.getSession().setAttribute("key2","v2");
        request.getServletContext().setAttribute("key3","value333");
        return  mv;
    }


    @RequestMapping("/demoJson")
    @ResponseBody
    public String mfrehbbferhting() {
        System.out.println("dhdnfsrrj====nrreserfk==========g");
        Map<String,Object>  map=new HashMap<>();
        map.put("resp_code","200");
        map.put("resp_msg","姚连霞");
        return map+"";
    }

    @RequestMapping("/upload")
    @ResponseBody
    public  Map<String,Object>   fileupload(List<MultipartFile>  filename, String username, String hobby) throws Exception{
       /* 文件上传*/
        for (MultipartFile  oneFile:filename) {
            System.out.println("name========="+oneFile.getOriginalFilename());
            String  oneFileName=oneFile.getOriginalFilename();
            String  oneFileSuffix=oneFileName.substring(oneFileName.lastIndexOf("."),oneFileName.length());
            String uid= Tqutil.uuid();
            String  Base_Dir="D:/uploadFiles/testSpringboot/";
            String resultFile=Base_Dir+"file_"+uid+oneFileSuffix;
            System.out.println("dfnftyjnh==============="+resultFile);
            oneFile.transferTo(new File(resultFile));
        }
        Map<String,Object>  map=new HashMap<>();
        map.put("resp_code","200");
        map.put("username",username);
        map.put("descr","如童话tjndrtj拟人的他");
        map.put("hobby",hobby);
        System.out.println("Tq001Controller.fileupload======"+map);
        return  map;
    }


}
